//
//  AcitivityDetail.m
//  12.30_homework_豆瓣电影MVC
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "AcitivityDetail.h"
#import "UIImageView+WebCache.h"
#import "ActivityModel.h"
#import "LXC_DataBaseHandle.h"


// MARK:VIEW_CENTER_X:屏中心x轴
#define VIEW_CENTER_X self.view.center.x

// MARK:VIEW_CENTER_Y:屏中心y轴
#define VIEW_CENTER_Y self.view.center.y

// MARK:HEIGHT:屏高
#define HEIGHT self.view.frame.size.height

// MARK:WIDTH:屏宽
#define WIDTH self.view.frame.size.width


@interface AcitivityDetail ()

@property (nonatomic, retain) NSDictionary *dic;
@property (nonatomic, retain) UIScrollView *scrollView;

// 自适应的高度结果
@property (nonatomic, assign) CGFloat myHeight;

// 取出当前字典
@property (nonatomic, retain) NSDictionary *dicNow;




@end

@implementation AcitivityDetail

- (void)dealloc {
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
 
    self.navigationItem.title = self.name;
 
    [self dataHandle];
    
    // 取出当前字典
    self.dicNow = _dic[@"events"][self.indexPath.row];
    
    [self creatPage];
    
    [self creatDetail];
    
}

- (void)creatPage {
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"btn_nav_share@2x.png"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(shareAction)];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:_scrollView];
    [_scrollView release];
    
/* * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * */
    // 求label高度
    
    // 文字的大小，描述文字大小的信息
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    
    // 通过文字大小获取文本的高度
    
    CGRect rect = [_dicNow[@"content"] boundingRectWithSize:CGSizeMake(375 - 40, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    // 自适应的高度结果
    self.myHeight = rect.size.height + 420;
    
/* * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * */
    
    
    _scrollView.contentSize = CGSizeMake(0, _myHeight);
}


- (void)shareAction {
    
    NSInteger loginState = [[LXC_DataBaseHandle shareDataBaseHandle] selectLoginStateFromUserTab:1];
    if (loginState) {
        
        
        // 获取当前的 model
        
        ActivityModel *model = [ActivityModel activityModel];
        model = _arrModel[_indexPath.row];
        
        BOOL flag = [[LXC_DataBaseHandle shareDataBaseHandle] selectModelBool:model];
        
        if (!flag) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否收藏" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
                
                [[LXC_DataBaseHandle shareDataBaseHandle] insertModel: model];
                
            }];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
            [alert addAction:action1];
            [alert addAction:action2];
            
            [self presentViewController:alert animated:YES completion:nil];
            
        }else {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该活动已经被收藏过" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
            
        }
    }else {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请登陆后查看" preferredStyle:(UIAlertControllerStyleAlert)];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:nil];

    }

    
    
    
    
    
}

- (void)creatDetail {
    
   
    
    // 标题
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 30)];
    title.text = _dicNow[@"title"];
    title.font = [UIFont systemFontOfSize:22];
    [_scrollView addSubview:title];
    [title release];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 70, 120, 180)];
    [image sd_setImageWithURL:[NSURL URLWithString:_dicNow[@"image"]]];
    [_scrollView addSubview:image];
    [image release];
    
    
    // 时间
    UIImageView *imageTime = [[UIImageView alloc] initWithFrame:CGRectMake(150, 70, 30, 30)];
    imageTime.image = [[UIImage imageNamed:@"icon_date@2x.png"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    [_scrollView addSubview:imageTime];
    
    UILabel *time = [[UILabel alloc] initWithFrame:CGRectMake(180, 70, 200, 30)];
    NSString *str1 = _dicNow[@"begin_time"];
    NSString *str2 = _dicNow[@"end_time"];
    time.text = [NSString stringWithFormat:@"%@ -- %@", [str1 substringWithRange:NSMakeRange(5, 11)], [str2 substringWithRange:NSMakeRange(5, 11)]];
    [_scrollView addSubview:time];
    
    
    // 所有者
    UIImageView *imageName = [[UIImageView alloc] initWithFrame:CGRectMake(150, 110, 30, 30)];
    imageName.image = [UIImage imageNamed:@"icon_sponsor_blue@2x.png"];
    [_scrollView addSubview:imageName];
    
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(180, 110, 200, 30)];
    name.text = _dicNow[@"owner"][@"name"];
    [_scrollView addSubview:name];
    
    
    // 类型
    UIImageView *category_name_Image = [[UIImageView alloc] initWithFrame:CGRectMake(150, 150, 30, 30)];
    category_name_Image.image = [UIImage imageNamed:@"icon_catalog@2x.png"];
    [_scrollView addSubview:category_name_Image];
    
    UILabel *category_name = [[UILabel alloc] initWithFrame:CGRectMake(180, 150, 200, 30)];
    category_name.text = [NSString stringWithFormat:@"类型：%@", _dicNow[@"category_name"]];
    [_scrollView addSubview:category_name];
    
    
    // 地址
    UIImageView *imageAddress = [[UIImageView alloc] initWithFrame:CGRectMake(150, 190, 30, 30)];
    imageAddress.image = [UIImage imageNamed:@"icon_spot@2x.png"];
    [_scrollView addSubview:imageAddress];
    
    UILabel *address = [[UILabel alloc] initWithFrame:CGRectMake(180, 190, 180, 60)];
    address.text = _dicNow[@"address"];
    address.numberOfLines = 0;
    address.lineBreakMode = NSLineBreakByClipping;
    [_scrollView addSubview:address];
    
    //
    UILabel *myTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 270, 100, 30)];
    myTitle.text = @"活动介绍";
    myTitle.font = [UIFont boldSystemFontOfSize:22];
    [_scrollView addSubview:myTitle];
    
    
    
    
    //
    UILabel *content = [[UILabel alloc] initWithFrame:CGRectMake(20, 320, WIDTH - 40, _myHeight - 350)];
    content.text = _dicNow[@"content"];
    content.numberOfLines = 0;
    [_scrollView addSubview:content];
    [content release];
    content.numberOfLines = 1000000;
    
    
    
    

    
    
}

- (void)dataHandle {
    // 路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activitylist" ofType:@"txt"];
    // 转成数据
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    // 解析数据
    self.dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:&error];
    if (error) {
        NSLog(@"%@", error);
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
